package system;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import system.common.ApplicationContextBean;
import system.common.Arguments;
import system.common.Constants;
import system.service.DB10_iDeskService;
import system.service.DB1_WebkeeperMigrateService;
import system.service.DB1_WebkeeperService;
import system.service.DB2_MsgIService;
import system.service.DB3_DbIService;
import system.service.DB4_PrivacyIService;
import system.service.DB5_NetKeyService;
import system.service.DB6_RedcastleService;
import system.service.DB7_PowerBrokerService;
import system.service.DB8_TxLoggingService;
import system.service.DB9_PortalLoginService;
import system.util.DateUtil;
import system.util.StringUtil;

public class Execute {
	private static Logger _log = LoggerFactory.getLogger(Execute.class);
	private static final String _LogPrefixStr = "test Log> ";
	
	public static void main(String[] args) {
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		_log.debug(_LogPrefixStr + "|" + " start");
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		
	    Execute exc = new Execute();
	    exc.doMain(args);
	    
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		_log.debug(_LogPrefixStr + "|" + " successful");
		_log.debug(_LogPrefixStr + "|" + " total: " + Constants.totalRecord + "(" + (Constants.rsTime + Constants.fileMakeTime + Constants.fileCopyTime) + " second)");
		_log.debug(_LogPrefixStr + "|" + " rs(" + Constants.rsTime + " second) > file make(" + Constants.fileMakeTime + " second) > file copy(" + Constants.fileCopyTime + " second) ");
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
	    System.exit(1);
	}
	
	private void doMain(String[] args) {
		Arguments arguments = new Arguments();

		arguments.setParameter0(args[0]);
		switch (args.length) {
		case 1:
			arguments.setParameter1("DAY");
			if (args[0].equals("TX_LOGGING_01")) {
				arguments.setParameter2(DateUtil.dateAddFormat(Calendar.DATE, -2, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
				arguments.setParameter3(DateUtil.dateAddFormat(Calendar.DATE, -2, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
			} else {
				arguments.setParameter2(DateUtil.dateAddFormat(Calendar.DATE, -1, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
				arguments.setParameter3(DateUtil.dateAddFormat(Calendar.DATE, -1, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
			}
			arguments.setDurationValue(1);
			break;
		case 2:
			if (!args[1].equals("DAY") && !args[1].equals("HOUR") && !args[1].equals("MIN")) {
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.error(_LogPrefixStr + "|" + " incorrect command :: argument-2");
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				System.exit(1);
			}
			arguments.setParameter1(args[1]);
			arguments.setParameter2(DateUtil.dateAddFormat(Calendar.DATE, -1, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
			arguments.setParameter3(DateUtil.dateAddFormat(Calendar.DATE, -1, Constants.simDf_Day.format(new Date(Constants.currentTime)), "yyyyMMdd"));
			arguments.setDurationValue(1);
			break;
		case 4:
			if (!args[1].equals("DAY") && !args[1].equals("HOUR") && !args[1].equals("MIN")) {
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.error(_LogPrefixStr + "|" + " incorrect command :: argument-2");
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				System.exit(1);
			}
			if ((args[1].equals("DAY") && args[2].length() < 8) || (args[1].equals("HOUR") && args[2].length() < 10) || (args[1].equals("MIN") && args[2].length() < 12)) {
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.error(_LogPrefixStr + "|" + " incorrect command :: argument-3");
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				System.exit(1);
			}
			if (!StringUtil.isNumber(args[3])) {
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.error(_LogPrefixStr + "|" + " incorrect command :: argument-4");
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				System.exit(1);
			}
			Date d;
			String args1 = args[1];
			String args2 = "";
			String args3 = "";
			try {
				if (args1.equals("DAY")) {
					d = Constants.simDf_Day.parse(args[2].substring(0, 8));
					DateUtil.setDefaultDateFormat("yyyyMMdd");
					args2 = DateUtil.dateAddFormat(Calendar.DATE, -(Integer.parseInt(args[3]) - 1), d);
					args3 = DateUtil.dateAddFormat(Calendar.DATE, 0, d);
				} else if (args1.equals("HOUR")) {
					d = Constants.simDf_Hour.parse(args[2].substring(0, 10));
					DateUtil.setDefaultDateFormat("yyyyMMddHH");
					args2 = DateUtil.dateAddFormat(Calendar.HOUR, -(Integer.parseInt(args[3]) - 1), d);
					args3 = DateUtil.dateAddFormat(Calendar.HOUR, 0, d);
				} else if (args1.equals("MIN")) {
					d = Constants.simDf_Min.parse(args[2].substring(0, 12));
					DateUtil.setDefaultDateFormat("yyyyMMddHHmm");
					args2 = DateUtil.dateAddFormat(Calendar.MINUTE, -(Integer.parseInt(args[3]) - 1), d);
					args3 = DateUtil.dateAddFormat(Calendar.MINUTE, 0, d);
				}
				arguments.setParameter1(args1);
				arguments.setParameter2(args2);
				arguments.setParameter3(args3);
				arguments.setDurationValue(Integer.parseInt(args[3]));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			break;
		default:
			_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			_log.error(_LogPrefixStr + "|" + " incorrect command");
			_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			System.exit(1);
			break;
		}
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		_log.debug(_LogPrefixStr + "|" + " arguments 1 :: " + arguments.getParameter0());
		_log.debug(_LogPrefixStr + "|" + " arguments 2 :: " + arguments.getParameter1());
		_log.debug(_LogPrefixStr + "|" + " arguments 3 :: " + arguments.getParameter2());
		_log.debug(_LogPrefixStr + "|" + " arguments 4 :: " + arguments.getParameter3());
		_log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		
		/* Time Test */
//		String startTime = arguments.getParameter2();
//		String endTime = arguments.getParameter3();
//		String tempStartTime = startTime;
//		String tempEndTime;
//		
//		Date d;
//		DateUtil.setDefaultDateFormat("yyyyMMddHHmm");
//		try {
//			for (int i = 1; i < 49; i++) {
//				d = Constants.simDf_Min.parse(startTime);
//				tempEndTime = DateUtil.dateAddFormat(Calendar.MINUTE, (i * 30), d);
//				arguments.setParameter2(tempStartTime, 1);
//				if (i == 48) {
//					arguments.setParameter3(endTime, 1);
//				} else {
//					arguments.setParameter3(tempEndTime, 1);
//				}
//				System.out.println(arguments.getParameter2() + " ~ " + arguments.getParameter3());
//				int time = Integer.parseInt(arguments.getParameter2().substring(11, 13));
//				if (time % 2 != 0) {
//					time = time - 1;
//				}
//				String tableTimeName = "0" + time;
//				System.out.println(">> "  + tableTimeName.substring((tableTimeName.length() - 2), tableTimeName.length()));
//				d = Constants.simDf_Min.parse(tempEndTime);
//				tempStartTime = DateUtil.dateAddFormat(Calendar.MINUTE, 1, d);
//				System.out.println("-------------");
//			}
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		System.exit(1);
		
		try {
			ApplicationContextBean bean = ApplicationContextBean.getInstance();

			if (args[0].equals("WEBKEEPER_01")) {
				DB1_WebkeeperService webkeeperService = (DB1_WebkeeperService) bean.getBean("DB1_WebkeeperService");
				webkeeperService.webkeeper_01(arguments);
			} else if (args[0].equals("WEBKEEPER_02")) {
				DB1_WebkeeperService webkeeperService = (DB1_WebkeeperService) bean.getBean("DB1_WebkeeperService");
				webkeeperService.webkeeper_02(arguments);
			} else if (args[0].equals("WEBKEEPER_MIGRATE_01")) {
				DB1_WebkeeperMigrateService webkeeperMigrateService = (DB1_WebkeeperMigrateService) bean.getBean("DB1_WebkeeperMigrateService");
				webkeeperMigrateService.webkeeper_migrate_01(arguments);
			} else if (args[0].equals("WEBKEEPER_MIGRATE_02")) {
				DB1_WebkeeperMigrateService webkeeperMigrateService = (DB1_WebkeeperMigrateService) bean.getBean("DB1_WebkeeperMigrateService");
				webkeeperMigrateService.webkeeper_migrate_02(arguments);
			} else if (args[0].equals("MSG_I_01")) {
				DB2_MsgIService msgIService = (DB2_MsgIService) bean.getBean("DB2_MsgIService");
				msgIService.msg_i_01(arguments);
			} else if (args[0].equals("MSG_I_02")) {
				DB2_MsgIService msgIService = (DB2_MsgIService) bean.getBean("DB2_MsgIService");
				msgIService.msg_i_02(arguments);
			} else if (args[0].equals("MSG_I_03")) {
				DB2_MsgIService msgIService = (DB2_MsgIService) bean.getBean("DB2_MsgIService");
				msgIService.msg_i_03(arguments);
			} else if (args[0].equals("DB_I_01")) {
				DB3_DbIService dbIService = (DB3_DbIService) bean.getBean("DB3_DbIService");
				dbIService.db_i_01(arguments);
			} else if (args[0].equals("PRIVACY_I_01")) {
				DB4_PrivacyIService privacyIService = (DB4_PrivacyIService) bean.getBean("DB4_PrivacyIService");
				privacyIService.privacy_i_01(arguments);
			} else if (args[0].equals("NET_KEY_01")) {
				DB5_NetKeyService netKeyService = (DB5_NetKeyService) bean.getBean("DB5_NetKeyService");
				netKeyService.net_key_01(arguments);	
			} else if (args[0].equals("NET_KEY_02")) {
				DB5_NetKeyService netKeyService = (DB5_NetKeyService) bean.getBean("DB5_NetKeyService");
				netKeyService.net_key_02(arguments);
			} else if (args[0].equals("REDCASTLE_01")) {
				DB6_RedcastleService redcastleService = (DB6_RedcastleService) bean.getBean("DB6_RedcastleService");
				redcastleService.redcastle_01(arguments);
			} else if (args[0].equals("REDCASTLE_02")) {
				DB6_RedcastleService redcastleService = (DB6_RedcastleService) bean.getBean("DB6_RedcastleService");
				redcastleService.redcastle_02(arguments);
			} else if (args[0].equals("REDCASTLE_03")) {
				DB6_RedcastleService redcastleService = (DB6_RedcastleService) bean.getBean("DB6_RedcastleService");
				redcastleService.redcastle_03(arguments);
			} else if (args[0].equals("POWERBROKER_01")) {
				DB7_PowerBrokerService powerBrokerService = (DB7_PowerBrokerService) bean.getBean("DB7_PowerBrokerService");
				powerBrokerService.power_broker_01(arguments);
			} else if (args[0].equals("POWERBROKER_02")) {
				DB7_PowerBrokerService powerBrokerService = (DB7_PowerBrokerService) bean.getBean("DB7_PowerBrokerService");
				powerBrokerService.power_broker_02(arguments);
			} else if (args[0].equals("TX_LOGGING_01")) {
				DB8_TxLoggingService txLoggingService = (DB8_TxLoggingService) bean.getBean("DB8_TxLoggingService");
				txLoggingService.tx_logging_01(arguments);
			} else if (args[0].equals("PORTAL_LOGIN_01")) {
				DB9_PortalLoginService portalLoginService = (DB9_PortalLoginService) bean.getBean("DB9_PortalLoginService");
				portalLoginService.portal_login_01(arguments);
			} else if (args[0].equals("IDESK_01")) {
				DB10_iDeskService iDeskService = (DB10_iDeskService) bean.getBean("DB10_iDeskService");
				iDeskService.idesk_01(arguments);
			} else {
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				_log.error(_LogPrefixStr + "|" + " incorrect command");
				_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				System.exit(1);
			}
		} catch (Exception e) {
			_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			_log.error(_LogPrefixStr + "|" + " exception :: ", e);
			_log.error(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
	}
}
